﻿package howtodo{
	import flash.events.Event;
	import flash.geom.Bezier;
	import flash.geom.Intersection;
	import flash.geom.Line;
	import flash.geom.Point;
	import flash.geom.Rectangle;	
	import org.papervision3d.core.math.Number3D

	public class Step08BounceMod extends BezierUsage {
		
		//private static const DESCRIPTION:String = "<B>Bounse: detect intersection (not finished methods)</B><BR/><BR/>drag control points";
		
		public const ball:PointView = new PointView();
		private const stageRectangle:Rectangle = new Rectangle();
		private const stepLine:Line = new Line();
		
		private var speedX:Number=0;
		private var speedY:Number=0;
		
		public var segmentCenters:Array
		
		public function Step08BounceMod () {
			super();
		}
		
		override protected function init():void {
			super.init();
			
			//initDescription(DESCRIPTION);
			
			addChild(ball);
			ball.x = -1;
			
			addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			addEventListener(Event.RESIZE, onResize);
			
			start.x = 25;
			start.y = 25;
			
			control.x = 125;
			control.y = 75;
			
			end.x = 175;
			end.y = 175;
			
			onResize();
			redraw();
		}
		
		protected function onResize(event:Event=null):void {
			stageRectangle.width = stage.stageWidth*.25;
			stageRectangle.height = stage.stageHeight*.35;
		}
		
		protected function enterFrameHandler(event:Event):void {
			if (stageRectangle.containsPoint(ball.point)) {
				moveBall();
			} else {
				initBallMotion();
			}
		}
		
		private function initBallMotion ():void {
			ball.x = 1;
			ball.y = stageRectangle.height-1;
			speedX = 10; // Math.random()+10;
			speedY =-10; //Math.random()-11;
		}
		
		private function moveBall():void {
			stepLine.start = ball.position;
			ball.x+=speedX;
			ball.y+=speedY;
			stepLine.end = ball.position;
			var intersection:Intersection = bezier.intersectionLine(stepLine);
			if (intersection) {
				
				//trace(intersection.currentTimes, intersection.targetTimes, intersection.isCoincidence, bezier.getTimeByDistance(200));
				
				var time:Number = intersection.currentTimes[0];
				var fulcrum:Point = bezier.getPoint(time);
				var tangentAngle:Number = bezier.getTangentAngle(time);
				
				var angleDist:Number = (tangentAngle-stepLine.angle)%Math.PI;
				
				graphics.lineStyle(0, 0x0000FF, 1);
				drawLine(stepLine);
				stepLine.angleOffset(angleDist*2, fulcrum);
				graphics.lineStyle(0, 0x00FF00, 1) ; 
				drawLine(stepLine);
				
				speedX=stepLine.end.x - stepLine.start.x; 
				speedY=stepLine.end.y - stepLine.start.y;
				ball.x = stepLine.end.x;
				ball.y = stepLine.end.y;
			}
		}
		
		override protected function onPointMoved(event:Event=null):void {
			redraw();
		}
		
		private function redraw ():void {
			dispatchEvent(new Event("redraw"));
			graphics.clear();
			graphics.lineStyle(0, 0xFF0000, 1);
			drawBezier(bezier);
			graphics.lineStyle(0, 0xFF0000, .3);
			//drawRectangle(bezier.bounds);
			//drawRectangle(stepLine.bounds);
			drawRectangle(stageRectangle)
			segmentCenters = new Array();
			
			var segmentsNum:uint = 10
			var times:Array = bezier.getTimesSequence(20)
			//trace("times", times.length,":", times.toString())
			for (var i:int = 1; i < times.length; i++) {
				var p1:Point = bezier.getPoint(times[i - 1]);
				var p2:Point = bezier.getPoint(times[i]);
				drawSegment2D(p1, p2)
				//trace(i+":", p0.x, p0.y,"-",p.x, p.y)
			}
			
			drawSegment2D(bezier.getPoint(times[i - 1]), bezier.end)
		}
		
		private function drawSegment2D(p1:Point, p2:Point):void {
			//graphics.lineStyle(2, 0x00ff00, 1)
			//graphics.moveTo(p0.x, p0.y)
			//graphics.lineTo(p.x, p.y)
			
			var dx:Number = p2.x - p1.x
			var dy:Number = p2.y - p1.y
			var angle:Number = Math.atan2(dy, dx)*Number3D.toDEGREES
			var distance:Number = Math.sqrt(dx * dx + dy * dy)
			var cx:int = p1.x + dx/2
			var cy:int = p1.y + dy/2
			//graphics.drawCircle(cx, cy, 5)

			segmentCenters.push(new Segment2D(new Point(cx, cy), distance, angle));
		}
		
	}
}